﻿using System;
using System.Collections.Generic;
using System.Web.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Linq;
using System.Text;

using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 站点导航
	/// </summary>
	public class SiteNavList : BaseRepeaterList
	{
		/// <summary>
		/// 全部站点
		/// </summary>
		private static readonly List<Site> _SiteList = Sites.GetItems();
		/// <summary>
		/// 更多站点
		/// </summary>
		protected Repeater CtrMoreList;

		/// <summary>
		/// 页面加载 OnInit
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			CtrMoreList.DataBinding +=new EventHandler(CtrMoreList_DataBinding);
			base.OnInit(e);
		}
		/// <summary>
		/// 获取主站点ID
		/// </summary>
		public virtual int GetMainSiteId
		{
			get { return int.Parse(StringExtensions.GetEffectiveValueOrDefault(WebConfigurationManager.AppSettings["MainSiteId"], "13")); }
		}
		/// <summary>
		/// 获取导航显示的站点ID		
		/// </summary>
		public virtual int[] GetShowSite
		{
			get
			{
				var sites = StringExtensions.GetEffectiveValueOrDefault(WebConfigurationManager.AppSettings["ShowSite"], "13");
				if (string.IsNullOrEmpty(sites)) return null;
				List<int> list = new List<int>();
				foreach (var str in sites.Split(','))
				{
					list.Add(int.Parse(str));
				}
				return list.ToArray();
			}
		}
		/// <summary>
		/// 获取导航的更多站点ID
		/// </summary>
		public virtual int[] GetShowMoreSite
		{
			get
			{
				var sites = StringExtensions.GetEffectiveValueOrDefault(WebConfigurationManager.AppSettings["ShowMoreSite"], "");
				if (string.IsNullOrEmpty(sites)) return null;
				List<int> list = new List<int>();
				foreach (var str in sites.Split(','))
				{
					list.Add(int.Parse(str));
				}
				return list.ToArray();
			}
		}
		/// <summary>
		/// 是否显示更多
		/// </summary>
		public virtual bool IsShowMore
		{
			get
			{
				return GetShowMoreSite == null;
			}
		}
		/// <summary>
		/// 获取导航站点列表
		/// </summary>
		/// <returns></returns>
		public override object GetListData()
		{
			return GetShowSite.SelectMany(s => _SiteList.Where(p => p.SiteId == s)).ToList();
		}

		public override string TitleField { get{ return null;} }

		/// <summary>
		/// 导航绑定更多站点
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public virtual void CtrMoreList_DataBinding(object sender, EventArgs e)
		{
			CtrMoreList.DataSource = GetMoreListData();
		}
		/// <summary>
		/// 获取导航更多站点列表
		/// </summary>
		/// <returns></returns>
		public virtual List<Site> GetMoreListData()
		{
			if (GetShowMoreSite == null)
				return null;
			return GetShowMoreSite.SelectMany(s => _SiteList.Where(p => p.SiteId == s)).ToList();
		}

		public override string ItemUrl
		{
			get { return string.Format("http://{0}", Convert.ToString(Eval("MainDomain"))); }
		}



		public override string ItemTitle
		{
			get
			{
				var siteId = (int)Eval("SiteId");
				if (siteId == GetMainSiteId) return Convert.ToString(Eval("SiteName"));

				return Convert.ToString(Eval("SiteName")).Replace("网", "");
			}
		}		
	}
}
